package cn.edu.neusoft.amicooldemo;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import cn.edu.neusoft.amicooldemo.fragment.CArticleFragment;
import cn.edu.neusoft.amicooldemo.fragment.CProjectFragment;
import cn.edu.neusoft.amicooldemo.fragment.CTcaseFragment;
import cn.edu.neusoft.amicooldemo.fragment.CTwareFragment;
import cn.edu.neusoft.amicooldemo.fragment.CVideoFragment;
import cn.edu.neusoft.amicooldemo.widget.PagerSlidingTabStrip;

public class CollectActivity extends AppCompatActivity {
    private CVideoFragment cVideoFragment;
    private CArticleFragment cArticleFragment;
    private CTwareFragment cTwareFragment;
    private CTcaseFragment cTcaseFragment;
    private CProjectFragment cProjectFragment;
    /**
     * PagerSlidingTabStrip的实例
     */
    private PagerSlidingTabStrip tabs;

    /**
     * 获取当前屏幕的密度
     */
    private DisplayMetrics dm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        initView();
    }
    private void initView() {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) findViewById(R.id.pager_collect);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs_collect);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }

    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#45c01a"));// #45c01a
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor("#45c01a"));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    // FragmentPagerAdapter FragmentStatePagerAdapter //不能用FragmentPagerAdapter


    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = { "视频","课件","文章","案例","项目" };

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:

                    if (null == cVideoFragment) {
                        cVideoFragment = new CVideoFragment();
                    }

                    return cVideoFragment;

                case 1:

                    if (null == cTwareFragment) {
                        cTwareFragment = new CTwareFragment();
                    }

                    return cTwareFragment;

                case 2:

                    if (null == cArticleFragment) {
                        cArticleFragment = new CArticleFragment();
                    }

                    return cArticleFragment;
                case 3:

                    if (null == cTcaseFragment) {
                        cTcaseFragment = new CTcaseFragment();
                    }

                    return cTcaseFragment;
                case 4:

                    if (null == cProjectFragment) {
                        cProjectFragment = new CProjectFragment();
                    }

                    return cProjectFragment;

                default:
                    return null;
            }
        }

    }
}
